package org.budo.support.spring.web.util;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;

import org.budo.support.lang.util.ReflectUtil;
import org.springframework.web.method.HandlerMethod;

/**
 * @author lmw
 */
public class HandlerUtil {
    /**
     * Method对象转成String的缓存
     */
    private static final Map<Method, String> _METHOD_URI_CACHE_MAP = new ConcurrentHashMap<Method, String>();

    /**
     * getHandlerRequestURI
     */
    public static String getHandlerRequestURI(HttpServletRequest request, Object handler) {
        if (null == handler || !(handler instanceof HandlerMethod)) {
            return request.getRequestURI();
        }

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        String methodUri = _METHOD_URI_CACHE_MAP.get(method);
        if (null != methodUri) {
            return methodUri;
        }

        String methodToString = ReflectUtil.methodToString(method);
        _METHOD_URI_CACHE_MAP.put(method, methodToString);
        return methodToString;
    }
}